package com.sos.service;

import com.sos.vo.SetInfo;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

/**
 * 系统设置
 * SharedPreferences类参考
 * http://developer.android.com/reference/android/content/SharedPreferences.html
 * 
 * Using Shared Preferences
   http://developer.android.com/guide/topics/data/data-storage.html#pref
   
   Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File （三） —— SharePreferences 
   
   http://www.cnblogs.com/wisekingokok/archive/2011/09/16/2177833.html
 * **/
public class SetManager {
	private final String TAG = SetManager.class.getSimpleName();
	private final static String PREFS_NAME = "sos_set";
	private final static String SOS_PHONE = "SOS_PHONE";// 求救人电话
	private final static String MY_PHONE = "MY_PHONE";// 我的电话
	private final static String MY_NAME = "MY_NAME";// 我的姓名
	private final static String DEFALUT_EAMIL = "DEFALUT_EAMIL";// 求救人邮箱
	private final static String WWW_IP = "WWW_IP";
	private final static String WWW_PORT = "WWW_PORT";
	private Context context;
	private SharedPreferences settings;

	public SetManager(Context context) {
		this.context = context;
		settings = context.getSharedPreferences(PREFS_NAME, 0);
	}

	public void save(String sos_phone, String my_hone, String my_name,
			String defalut_email, String www_ip, String www_port)

	{
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(SOS_PHONE, sos_phone);
		editor.putString(MY_PHONE, my_hone);
		editor.putString(MY_NAME, my_name);
		editor.putString(DEFALUT_EAMIL, defalut_email);
		editor.putString(WWW_IP, www_ip);
		editor.putString(WWW_PORT, www_port);
	}

	public void save(SetInfo set)
	{
		Log.d(TAG, "save");
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(SOS_PHONE, set.getSos_phone());
		editor.putString(MY_PHONE, set.getMy_hone());
		editor.putString(MY_NAME, set.getMy_name());
		editor.putString(DEFALUT_EAMIL, set.getDefalut_email());
		editor.putString(WWW_IP, set.getWww_ip());
		editor.putString(WWW_PORT, set.getWww_port());
		
		if(editor.commit()){
			Toast.makeText(context, "保存成功", Toast.LENGTH_LONG).show();
		}
	}
	public SetInfo get()
	{
		Log.d(TAG, "get");
		SetInfo set=new SetInfo();
		set.setDefalut_email(settings.getString(DEFALUT_EAMIL, ""));
		set.setMy_hone(settings.getString(MY_PHONE, ""));
		set.setMy_name(settings.getString(MY_NAME, ""));
		set.setSos_phone(settings.getString(SOS_PHONE, ""));
		set.setWww_ip(settings.getString(WWW_IP, ""));
		set.setWww_port(settings.getString(WWW_PORT, ""));
		return set;
	}
}
